package cp2013;

import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;

import javax.swing.*;
/**
 *
 * @author AcerXPS
 */
public class LoginUI extends JPanel{

    private BufferedImage img = null;
    private File f;
    private SpringLayout layout = new SpringLayout();
    private JLabel lblUser = new JLabel("Username:");
    private JLabel lblPass = new JLabel("Password:");
    private JTextField txtUser = new JTextField(15);
    private JPasswordField pwfPassword = new JPasswordField(15);
    private JButton btnLogin = new JButton("Login");
    private JButton btnRegister = new JButton("Register");


    @Override
    public void paintComponent(Graphics g){
        Graphics2D g2d = (Graphics2D) g;
        g2d.drawRenderedImage(img, AffineTransform.getTranslateInstance(0, 0));
    }
    
    public JButton getLoginButton(){
        return btnLogin;
    }

    public JButton getRegisterButton(){
        return btnRegister;
    }

    public JTextField getUserName(){
        return txtUser;
    }

    public JPasswordField getPasswordField(){
        return pwfPassword;
    }

    public void initiateLoginUI(){
        try {
            f = new File("Login.jpg");
            img = ImageIO.read(f);
        } catch (IOException e) {
            System.out.println(f.getAbsolutePath());
        } finally{
            
        layout.putConstraint(SpringLayout.WEST, lblUser, 250, SpringLayout.WEST, this);
        layout.putConstraint(SpringLayout.NORTH, lblUser, 250, SpringLayout.NORTH, this);
        layout.putConstraint(SpringLayout.WEST, txtUser, 5, SpringLayout.EAST, lblUser);
        layout.putConstraint(SpringLayout.NORTH, txtUser, 250, SpringLayout.NORTH, this);

        layout.putConstraint(SpringLayout.WEST, lblPass, 250, SpringLayout.WEST, this);
        layout.putConstraint(SpringLayout.NORTH, lblPass, 280, SpringLayout.NORTH, this);
        layout.putConstraint(SpringLayout.WEST, pwfPassword, 6, SpringLayout.EAST, lblPass);
        layout.putConstraint(SpringLayout.NORTH, pwfPassword, 280, SpringLayout.NORTH, this);

        layout.putConstraint(SpringLayout.WEST, btnLogin, 350, SpringLayout.WEST, this);
        layout.putConstraint(SpringLayout.NORTH, btnLogin, 320, SpringLayout.NORTH, this);
        layout.putConstraint(SpringLayout.WEST, btnRegister, 340, SpringLayout.WEST, this);
        layout.putConstraint(SpringLayout.NORTH, btnRegister, 350, SpringLayout.NORTH, this);

        setLayout(layout);
        add(lblUser);
        add(txtUser);
        add(lblPass);
        add(pwfPassword);
        add(btnLogin);
        add(btnRegister);
        setBackground(Color.white);
        }

    }


    public void addloginUIListener(ActionListener listener){
        btnLogin.addActionListener(listener);
        btnRegister.addActionListener(listener);
    }

    public void reset(){
        txtUser.setText(null);
        pwfPassword.setText(null);
    }
}
